package org.nahtanoj.driveutils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Logger;

public class DifferenceIdentifier {
	private static final Logger log = Logger.getLogger(DifferenceIdentifier.class.getName());
//	public DifferenceIdentifier(String q, String masterPrefix, String actualPrefix) throws IOException {
//		processQueue(q, masterPrefix, actualPrefix);
//	}
	/**
	 * @param master - directory on the home file system
	 * @param masterPrefix - prefix to remove
	 * @param actual - directory on Google Drive
	 * @param actualPrefix - prefix to remove
	 * @param queue - queue file to record progress
	 * @throws IOException
	 */
	public void createQueue(String master, String masterPrefix, String actual, String actualPrefix, String queue) throws IOException {
		HashMap<String, String>a = load(actual, actualPrefix);
		HashMap<String, String>m = load(master, masterPrefix);

		// Build a list of files in d but not in l
		// Build a list of files in l but not in d
		// List<String>dl = diff(d, l); // Not in master
		List<String>actualErrors = diff(m, a); // Not in actual

		// ld now contains a list of errors
		// We process each one - and if we can't we write out the error list
		writeQueue(queue, actualErrors);
		log.info("Actual:" + a.size() + " Master:" + m.size() + " Errors:" + actualErrors.size());
	}

	private List<String> diff(HashMap<String, String> l, HashMap<String, String> d) {
		List<String>error = new ArrayList<String>();
		for (String path : l.keySet()) {
			if (d.containsKey(path) && d.get(path).equals(l.get(path))) {
			} else {
				error.add(path);
			}
		}
		Collections.sort(error);
		return error;
	}

	private HashMap<String, String> load(String file, String prefix) throws IOException {
		HashMap<String, String>files = new HashMap<String, String>();

		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(file))));
		String line;
		while ((line = br.readLine()) != null) {
			String size = line.substring(0, line.indexOf(" "));
			String path = line.substring(line.indexOf(" ") + 1);
			if (path.startsWith(prefix)) {
				path = path.substring(prefix.length());
			}
			files.put(path, size);
		}
		br.close();
		return files;
	}

	/**
	 * @param q - name of the queue file
	 * @param masterPrefix - prefix to get to the files on Google Drive
	 * @param actualPrefix - prefix to get to the files on the local file system
	 * @throws IOException
	 */
	public void processQueue(String q, String masterPrefix, String actualPrefix) throws IOException {
		List<String>queue = new ArrayList<String>();
		List<String>error = new ArrayList<String>();
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(q))));
		String line;
		while ((line = br.readLine()) != null) {
			queue.add(line);
			error.add(line);
		}
		br.close();

		int success = 0;
		int failure = 0;
		int queued = error.size();
		DriveUtils du = new DriveUtils();
		for (String f : queue) {
			if (du.newFile(f, actualPrefix, masterPrefix) == true) {
				error.remove(f);
				writeQueue(q, error);
				success = success + 1;
			} else {
				failure = failure + 1;
			}
			log.info(success + failure + " of " + queued + " Success:" + success + " Failure:" + failure);
		}

		if (error.size() > 0) {
			writeQueue(q, error);
			log.info(error.size() + " items not successful");
		} else {
			log.info("All done");
		}
	}

	private void writeQueue(String queue, List<String>items) throws IOException {
		FileWriter fw = new FileWriter(queue);
		for (String s : items) {
			fw.write(s);
			fw.write("\n");
		}
		fw.close();
	}
}
